/* ***** BEGIN LICENSE BLOCK *****
 * Copyright (C) 2008-2011, The 100GET-E3-R3G Project Team.
 * 
 * This work has been funded by the Federal Ministry of Education
 * and Research of the Federal Republic of Germany
 * (BMBF Förderkennzeichen 01BP0775). It is part of the EUREKA project
 * "100 Gbit/s Carrier-Grade Ethernet Transport Technologies
 * (CELTIC CP4-001)". The authors alone are responsible for this work.
 *
 * See the file AUTHORS for details and contact information.
 * 
 * This file is part of MuLaViTo (Multi-Layer Visualization Tool).
 *
 * MuLaViTo is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License Version 3 or later
 * (the "GPL"), or the GNU Lesser General Public License Version 3 or later
 * (the "LGPL") as published by the Free Software Foundation.
 *
 * MuLaViTo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * or the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License and
 * GNU Lesser General Public License along with MuLaViTo; see the file
 * COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * ***** END LICENSE BLOCK ***** */
package mulavito.gui.components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Corner button for displaying a choosable miniature view of given layerviewers
 * 
 * @author Julian Ott
 */
@SuppressWarnings("serial")
public class SatelliteMultiViewerCorner extends SatelliteViewerCorner {
	private SpinnerListModel layer;
	private JSpinner spinner;

	public SatelliteMultiViewerCorner() {
		layer = new SpinnerListModel();
		layer.addChangeListener(new ChangeListener() {
			@SuppressWarnings("rawtypes")
			@Override
			public void stateChanged(ChangeEvent e) {
				// find viewer for satellite
				if (layer.getValue() instanceof LayerViewer)
					setViewer((LayerViewer) layer.getValue());
				else
					setViewer(null);
			}
		});
		spinner = new JSpinner(layer);
		// Workaround to fix the satellite jumping issue
		// caused by changeevent-setviewer-dolayout-setvalue-(layoutinvalid)
		spinner.setPreferredSize(new Dimension(105, 22));
		getSatellite().getContentPane().add(spinner, BorderLayout.NORTH);
		// by default disabled
		setEnabled(false);
	}

	/**
	 * sets the layer list for the spinner
	 * 
	 * @param layers
	 *            the layer list
	 */
	public void setLayerList(List<?> layers) {
		if (layers == null || layers.isEmpty()) {
			List<String> def = new ArrayList<String>();
			def.add("empty");
			layer.setList(def);
			setEnabled(false);
		} else {
			layer.setList(layers);
			setEnabled(true);
		}
	}
}
